package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.fengpanlong.entity.IdeaComment;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;
import com.fengpanlong.util.StringUtil;

public class IdeaCommentDao {
	private DBHelp<IdeaComment> db = new DBHelp<IdeaComment>();
	
	private class IdeaCommentRowMapper implements RowMapper<IdeaComment> {

		public IdeaComment mapperRow(ResultSet rs) throws SQLException {
			IdeaComment i = new IdeaComment();
			i.setId(rs.getString("id"));
			i.setContent(rs.getString("content"));
			i.setCreatetime(StringUtil.formateTime(rs.getString("createtime")));
			i.setIdeaId(rs.getString("ideaId"));
			i.setEmployeeId(rs.getString("employeeId"));
			return i;
		}
	}
	/**
	 * 通过想法id查找评论列表
	 * @param ideaId
	 * @return
	 */
	public List<IdeaComment> findIdeaCommentsByIdeaId(String ideaId) {
		String sql = "select id,content,createtime,ideaid,employeeid from t_ideacomment where ideaid = ? order by createtime desc";
		return db.executeQueryForList(sql, new IdeaCommentRowMapper(), ideaId);
	}
	/**
	 * 通过id删除评论
	 * @param id
	 */
	public void delById(String id) {
		String sql = "delete from t_ideacomment where id = ?";
		db.executeSQL(sql, id);
	}
	/**
	 * 保存评论
	 * @param ideaComment
	 */
	public void save(IdeaComment i) {
		String sql = "insert into t_ideacomment(id,content,createtime,ideaid,employeeid) values(?,?,?,?,?)";
		db.executeSQL(sql, i.getId(),i.getContent(),i.getCreatetime(),i.getIdeaId(),i.getEmployeeId());
	}
	/**
	 * 通过想法id删除评论
	 * @param ideaId
	 */
	public void delIdeaCommentsByIdeaId(String ideaId) {
		String sql = "delete from t_ideacomment where ideaId = ?";
		db.executeSQL(sql, ideaId);
	}
}
